package com.dbq.fast.business.order;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.business.login.NewLoginActivity;
import com.dbq.fast.business.login.bean.CustomProfile;
import com.dbq.fast.business.login.business.LoginBiz;
import com.dbq.fast.business.order.apdapter.OrderListAdapter;
import com.dbq.fast.business.order.api.OrderApi;
import com.dbq.fast.business.order.bean.OrderInfo;
import com.dbq.fast.business.product.ui.StatusView;
import com.dbq.fast.common.base.BaseActivity;
import com.dbq.fast.common.manager.DatabaseManager;
import com.dbq.fast.common.net.RemoteApiCallBack;

import java.util.List;

public class OrderListActivity extends BaseActivity {

    private final static int START_VPN_LOGIN_REQUEST_CODE = 100;

    private StatusView statusView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView orderRV;
    private OrderListAdapter adapter;
    private SwipeRefreshLayout.OnRefreshListener listener;
    private List<OrderInfo.OrderListBean> orderInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);


        statusView = (StatusView) findViewById(R.id.status_view);
        initRecycleView();
        initListener();
        initPullRefreshListener();

    }

    private void loadData() {
        statusView.setStatus(StatusView.LOADING);

        CustomProfile profile = DatabaseManager.getInstance().queryProfile();
        OrderApi.getOrderList(profile, new RemoteApiCallBack<OrderInfo>() {
            @Override
            public void onSuccess(OrderInfo orderInfo) {
                List<OrderInfo.OrderListBean> orderListBeans = orderInfo.getOrderList();
                if(orderListBeans != null && orderListBeans.size() != 0) {
                    if(statusView == null || adapter == null) return;
                    statusView.setStatus(StatusView.SUCCESS);
                    orderInfos = orderListBeans;
                    if(orderListBeans.size() > 10) {
                        orderInfos.add(null);
                    }
                    adapter.updateDatas(orderListBeans);
                }else {
                    if(statusView == null) return;
                    statusView.setStatus(StatusView.NO_ORDER_INFO);
                }
            }

            @Override
            public void onFail(int status, String message) {
                if(status == 402){
                    Toast.makeText(App.getContext(), ""+message, Toast.LENGTH_SHORT).show();
                    if(isFinishing()) return;
                    if(Build.VERSION.SDK_INT >= 17 && isDestroyed()) return;
                    LoginBiz loginBiz = new LoginBiz(OrderListActivity.this);
                    loginBiz.logOut();
                    Intent intent = new Intent(OrderListActivity.this, NewLoginActivity.class);
                    intent.putExtra("goLogin", true);
                    startActivityForResult(intent, START_VPN_LOGIN_REQUEST_CODE);
                }else{
                    if(statusView == null) return;
                    statusView.setStatus(StatusView.NET_ERROR);
                }
            }
        });

    }

    public void refreshList() {
        if (listener == null) return;
        listener.onRefresh();
    }

    private void initPullRefreshListener() {
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, android.R.color.holo_green_light, android.R.color.holo_blue_bright);

        listener = () -> {
            Log.e("vpn_log", "onRefresh");
            swipeRefreshLayout.setRefreshing(false);
            loadData();

        };

        swipeRefreshLayout.setOnRefreshListener(listener);
        refreshList();
    }

    private void initListener() {
        findViewById(R.id.back).setOnClickListener(view -> {
            onBackPressed();
        });
        statusView.setViewVisibleListener(visible -> {
            orderRV.setVisibility(visible);
        });
        statusView.setRefreshListener(() -> {
            loadData();
        });
    }

    private void initRecycleView() {
        orderRV = (RecyclerView) findViewById(R.id.order_list);

        //设置布局管理器
        orderRV.setLayoutManager(new LinearLayoutManager(this));
        orderRV.setHasFixedSize(true);

        adapter = new OrderListAdapter();
        //设置adapter
        orderRV.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == START_VPN_LOGIN_REQUEST_CODE){
            if(resultCode == RESULT_OK){
                loadData();
            }
        }
    }


}
